package com.firefly.admin.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.firefly.admin.common.constant.Constants;
import com.firefly.admin.common.vo.Result;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 响应工具类
 */
public class ResponseUtil {
    /**
     * 响应错误信息
     *
     * @param response 响应体
     * @param result   返回结果
     * @param <T>      结果泛型
     */
    public static <T> void returnFail(HttpServletResponse response, Result<T> result) {
        try {
            response.setCharacterEncoding(StringPool.UTF_8);
            response.setContentType(Constants.CONTENT_TYPE_JSON);
            response.setStatus(200);
            PrintWriter out = response.getWriter();
            String resultJson = JSONObject.toJSONString(result);
            out.write(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
